package com.example.administrator.u17.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.administrator.u17.R;
import com.example.administrator.u17.activity.ZhangJieActivity;
import com.example.administrator.u17.model.VIPData1;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by Administrator on 2016/11/14.
 */

public class VIPFragmentAdapter extends BaseAdapter{
    private Context context;
    private ArrayList<VIPData1.DataBean.ReturnDataBean.ComicsBean> mList;
    private String time;

    public VIPFragmentAdapter(Context context, ArrayList<VIPData1.DataBean.ReturnDataBean.ComicsBean> mList) {
        this.context = context;
        this.mList = mList;
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");

        time=simpleDateFormat.format(calendar.getTime());
    }

    @Override
    public int getCount() {
        return mList==null?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView=View.inflate(context, R.layout.vip_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }

        Picasso.with(context).load(mList.get(position).getCover()).into(holder.imageView);
        holder.tvTitle.setText(mList.get(position).getName());
//        holder.imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(context, ZhangJieActivity.class);
//                intent.putExtra("id",mList.get(position).getComicId());
//                context.startActivity(intent);
//            }
//        });

        holder.tvTime.setText("昨天"+time+"更新");
        StringBuilder builder=new StringBuilder();

        for (int i = 0; i <mList.get(position).getTags().size() ; i++) {

            builder.append(mList.get(position).getTags().get(i));

        }
        holder.tvType.setText(builder);
        holder.tvDescribe.setText(mList.get(position).getDescription());

        return convertView;
    }

    public static class ViewHolder{

        private TextView tvTitle,tvType,tvDescribe,tvTime;
        private ImageView imageView;
        public ViewHolder(View itemView) {
            tvDescribe= (TextView) itemView.findViewById(R.id.vip_item_tv_describe);
            tvTime= (TextView) itemView.findViewById(R.id.vip_item_tv_time);
            tvType= (TextView) itemView.findViewById(R.id.vip_item_tv_type);
            tvTitle= (TextView) itemView.findViewById(R.id.vip_item_tv_bookName);
            imageView= (ImageView) itemView.findViewById(R.id.vip_item_iv);
        }
    }
}
